package com.maotai.llzhou.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.http.client.config.RequestConfig;

import Decoder.BASE64Encoder;
import cn.hellotrue.HelloTrue;
import cn.hellotrue.HelloTrueHY;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maotai.llzhou.bean.MaotaiString;
import com.maotai.llzhou.bean.MaotaiWebCookie;
import com.maotai.llzhou.service.MaotaiService;
import com.maotai.llzhou.util.HttpClientUtilNew;
import com.maotai.llzhou.util.IPFactory;

public class MaoTaiRegisterThreadHY extends Thread {

	private MaotaiService maotaiService;
	private String token;


	public MaoTaiRegisterThreadHY(MaotaiService maotaiService, String token) {
		super();
		this.maotaiService = maotaiService;
		this.token = token;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public MaotaiService getMaotaiService() {
		return maotaiService;
	}

	public void setMaotaiService(MaotaiService maotaiService) {
		this.maotaiService = maotaiService;
	}

	@Override
	public void run() {
		HelloTrueHY ht = null;
		String telphone = null;
		try {
			MaotaiWebCookie mwc = new MaotaiWebCookie();
			BASE64Encoder encode = new BASE64Encoder();
			HttpClientUtilNew hcu = HttpClientUtilNew.getInstance();
			JSONArray jArray = IPFactory.getIpAndPort();
			JSONObject jj = (JSONObject) jArray.get(0);
			String ipAndPort = jj.getString("ip") + ":"
					+ jj.getString("port");
//			JSONObject jObject1 = IPFactory
//					.getKdByUrl(IPFactory.JL_IP_HTTP_URL_1);
//			JSONArray jArray = jObject1.getJSONArray("data");
//			JSONObject jj = (JSONObject) jArray.get(0);
//			String ipAndPort = jj.getString("IP") + ":"
//					+ jj.getString("Port");
			//现货区号码
			ht = new HelloTrueHY();
			telphone = ht.getPhone(token);
			System.out.println("获取到的号码为："+telphone);
			if(telphone.contains("htm")){
				return;
			}
			//先登录
			Map<String, String> maps = new HashMap<String, String>();
			//userCode=nickboowen&userPassword=MTk4NjA2MTg&loginType=name&loginSource=2&loginFlag=1
			maps.put("userCode", telphone);
			maps.put("userPassword", "MTIzNDU2");
			maps.put("loginType", "name");
			maps.put("loginSource", "2");
			maps.put("loginFlag", "1");
			//loginSource=2&loginType=name&userCode=13372007847&userPassword=cTE5ODYwNjE4 手机端
			String referer = "https://www.emaotai.cn/smartsales-b2c-web-pc/login";
			RequestConfig requestConfig = IPFactory.getKdRequestConfig(ipAndPort);
			try {
				mwc = hcu.sendHttpPostProxy(MaotaiString.LOGIN_URL_NEW,
						mwc, maps, requestConfig,referer);
			} catch (Exception e) {
				e.printStackTrace();
				return;
			}
			if(mwc.getContent().contains("504")){
				return;
			}
			if(mwc.getContent().contains("不存在")){
				
			}else{
				ht.addBlacklist(token, telphone);
				return;
			}
			
			//发送验证码到手机
			String send_url = MaotaiString.SEND_MOBILE_URL.replace("xxx", telphone);
			
			RequestConfig config = IPFactory.getKdRequestConfig(ipAndPort);
			mwc = hcu.sendHttpGetProxy(send_url, mwc, config, MaotaiString.REGISTER_REFERER_URL);
			//{"data":"发送成功","resultCode":0,"resultMsg":"success"}
			System.out.println(mwc.getContent());
			/**
				 * userName:13372007847
				password:cTE5ODYwNjE4
				mobile:13372007847
				verifyCode:123456
				confimpwd:cTE5ODYwNjE4
				appCode:newrretail
				channel:B2C
				 */
			//接收验证码
			//TODO
			String pwd = encode.encode(("a"+telphone).getBytes());
			pwd = pwd.replace("=", "");
			String message = ht.getMessage(token, telphone);
			System.out.println("获取到的验证码信息为：" + message);
			if("超时".equals(message)){
				try {
					throw new Exception("未获取到验证码");
				} catch (Exception e) {
				}
			}
			String regEx="[^0-9]";   
			Pattern p = Pattern.compile(regEx);   
			String telCode = p.matcher(message).replaceAll("").trim().substring(0,6);
			System.out.println("截取后的验证码信息为：" + telCode);
				Map<String, String> registerMap = new HashMap<String, String>();
				registerMap.put("userName", telphone);
				registerMap.put("password", pwd);
				registerMap.put("mobile", telphone);
				registerMap.put("verifyCode", telCode);
				registerMap.put("confimpwd", pwd);
				registerMap.put("appCode", "newrretail");
				registerMap.put("channel", "B2C");
				mwc = hcu.sendHttpPostProxy(MaotaiString.REGISTER_URL, mwc, registerMap ,config,MaotaiString.REGISTER_REFERER_URL);
//				mwc = hcu.sendHttpPostSendSms(MaotaiString.SENDSCM_URL, mwc, registerMap, "https://www.emaotai.cn/Register.aspx");
				
				System.out.println(mwc.getContent());
				//{"data":null,"resultCode":-1,"resultMsg":"验证码错误"}
				JSONObject jObject = JSONObject.parseObject(mwc.getContent());
				if(jObject!=null && "您的手机已注册".equals(jObject.getString("retmsg"))){
					System.out.println("手机号已注册");
					ht.addBlacklist(token, telphone);
				}else if(jObject!=null && "success".equals(jObject.getString("retmsg"))){
					System.out.println("手机号未注册，等待获取验证码....");
				}else{
					Map<String,String> map = new HashMap<String,String>();
					map.put("code", telphone);
					map.put("pwd", "a"+telphone);
					map.put("phone", telphone);
					maotaiService.insertZhanghaoWeb(map);
					ht.addBlacklist(token, telphone);
					
					Map<String,Object> zhanghao = new HashMap<String,Object>();
					zhanghao.put("code", telphone);
					zhanghao.put("pwd", pwd);
					zhanghao.put("phone", telphone);
					ht.addBlacklist(token, telphone);
//					MaotaiRegisterLoginThread mrl = new MaotaiRegisterLoginThread(maotaiService, zhanghao, ipAndPort);
//					mrl.run();
				}
		} catch (Exception e1) {
			e1.printStackTrace();
//			ht.addBlacklist(token, telphone);
		}
	}
}
